/*****  Component, by Ben Bi <prince.bi@gmail.com>  created at: 12/23/2009 4:10:45 PM  *****/

/********   Notes:  *******
 *  Two tags need to be replaced with the module name, they are:
 *  cleanUpQcFormItem
 *  CleanUpQcFormItem
 *  weave
 */

/*****  Component, by Ben Bi <prince.bi@gmail.com>  created at: 12/23/2009 4:10:45 PM  *****/

/********   Notes:  *******
 *  Two tags need to be replaced with the module name one by one, they are:
 *  cleanUpQcForm
 *  CleanUpQcForm
 *  clean_up_qc_form
 */


/***** variables declartion section, begin *****/
var cleanUpQcFormItemInputMode = wacFormInputMode.add;
var cleanUpQcFormItemObj = {};
var cleanUpQcFormObj = {};

/***** variables declartion section, end *****/

/***** init section, begin *****/
$(document).ready(
    function() {
        initCleanUpQcFormItemFormDialog();

        initCleanUpQcFormItemForm();

        bindCleanUpQcFormItemEvents();
    }
);

function bindCleanUpQcFormItemEvents()
{
    $("#cleanUpQcFormItemFormDialog").bind('dialogopen', function(event, ui)
    {
        initCleanUpQcFormItemFormData();
//        wacDebugLog("calling " + moduleName + ":" + wacModelObj.cleanUpQcFormItem.name);//
    });

    $("#cleanUpQcFormItemFormDialog").bind('dialogclose', function(event, ui)
    {
        $.validationEngine.closePrompt(".formError", true);
    });

    $("#cleanUpQcFormItem_btnSubmit").bind('click', function (e)
    {
        submitCleanUpQcFormItemForm();
    });

    $("#cleanUpQcFormItem_btnClose").bind('click', function (e)
    {
        $("#cleanUpQcFormItemFormDialog").dialog('close');
    });

    // fix dialog div didnt remove bug, remove it by this way
    $('#appStorehouseRightPane').bind('tabsremove', function(event, ui) {
        if(ui.panel.id == "t1042")
        {
            $("div[aria-labelledby='ui-dialog-title-cleanUpQcFormItemFormDialog']").remove();
            $("#cleanUpQcFormItemFormDialog").remove();
        }
    });
}


function initCleanUpQcFormItemFormDialog()
{
//    wacDebugLog("initCleanUpQcFormItemFormDialog.");
    $("#cleanUpQcFormItemFormDialog").dialog({
            bgiframe: true,
            modal: true,
            width: 850,
            height: 450,
            autoOpen: false,
            buttons: {},
            zIndex: 100
        });
}

function initCleanUpQcFormItemForm()
{
    $("#cleanUpQcFormItemForm").validationEngine();

    $('.wacFormContent .left, .wacFormContent input, .wacFormContent textarea, .wacFormContent select').focus(function(){
        $(this).parents('.wacFormContent').addClass("wacFormOver");
    }).blur(function(){
        $(this).parents('.wacFormContent').removeClass("wacFormOver");
    });

    $('.wacFormContentA .left, .wacFormContentA input, .wacFormContentA textarea, .wacFormContentA select').focus(function(){
        $(this).parents('.wacFormRow').addClass("wacFormOver");
    }).blur(function(){
        $(this).parents('.wacFormRow').removeClass("wacFormOver");
    });


}

function initCleanUpQcFormItemFormData()
{
    wacShowBlockUILoading("#cleanUpQcFormItemFormDialog");
    $('#cleanUpQcFormItem_roll_unit_code').empty();

    var params = {
        "data_format" : 'json',
        "clean_up_qc_form_id": cleanUpQcFormObj.id
    };

    $.ajax({
        url: BASE_URL + "cleanUpQcFormItem/getFormData",
        global: true,
        type: "GET",
        data: params,
        dataType: "json",
        success: function(jsonData){
            initCleanUpQcFormItemFormDataCallBack(jsonData);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            wacDebugLog("getFormData Error: " + $(this).dump()); // the options for this ajax request
        }
    });
}

function initCleanUpQcFormItemFormDataCallBack(jsonData)
{
   if(jsonData.info.status == wacOperationStatus.Error)
   {
       alert(jsonData.info.error_info);
   }
   else
    {
        var selectedIndex = 0;

        for(i=0;i<jsonData['items']['unit'].length;i++)
        {
            $('<option value="' + jsonData['items']['unit'][i].key +'">' + jsonData['items']['unit'][i].value +'</option>').appendTo("#cleanUpQcFormItem_roll_unit_code");
        }
   }

   setupCleanUpQcFormItemFormDefaultValues(jsonData['items']['default']);

   wacHideBlockUI("#cleanUpQcFormItemFormDialog");

//   wacDebugLog($.dump(jsonData));
}

function setupCleanUpQcFormItemFormDefaultValues(defaultValueObj)
{
    $("#cleanUpQcFormItem_clean_up_qc_form_id").attr("value", cleanUpQcFormObj.id);
    $("#cleanUpQcFormItem_clean_up_qc_form_code").attr("innerHTML", cleanUpQcFormObj.code);
    $("#cleanUpQcFormItem_production_order_code").attr("innerHTML", cleanUpQcFormObj.production_order_code);
    $("#cleanUpQcFormItem_clean_up_form_code").attr("innerHTML", cleanUpQcFormObj.clean_up_form_code);
    $("#cleanUpQcFormItem_goods_category_name").attr("innerHTML", cleanUpQcFormObj.goods_category_name);

    if(cleanUpQcFormItemInputMode == wacFormInputMode.add)   // use default values
    {
        $("#cleanUpQcFormItem_id").attr("value", 0);
        $("#cleanUpQcFormItem_roll_unit_code option:[value='"+ defaultValueObj.roll_unit_code +"']").attr('selected', 'selected');
    }
    else  // use edit obj values
    {
        $("#cleanUpQcFormItem_id").attr("value", cleanUpQcFormItemObj.id);
        $("#cleanUpQcFormItem_horizontal_shrink").attr("value", cleanUpQcFormItemObj.horizontal_shrink);
        $("#cleanUpQcFormItem_vertical_shrink").attr("value", cleanUpQcFormItemObj.vertical_shrink);
        $("#cleanUpQcFormItem_roll").attr("value", cleanUpQcFormItemObj.roll);

        $("#cleanUpQcFormItem_ph_value").attr("value", cleanUpQcFormItemObj.ph_value);
        $("#cleanUpQcFormItem_is_beveled").attr("checked", (cleanUpQcFormItemObj.is_beveled==1));

        $("#cleanUpQcFormItem_roll_unit_code option:[value='"+ cleanUpQcFormItemObj.roll_unit_code +"']").attr('selected', 'selected');
    }

//wacDebugLog($.dump(cleanUpQcFormItemObj));
//    wacDebugLog($.dump(cleanUpQcFormObj));
//    wacDebugLog($.dump(defaultValueObj));
//    wacDebugLog("cleanUpQcFormItemInputMode: " + cleanUpQcFormItemInputMode);

}
/***** init section, end *****/


/**************  interaction section, begin  ***************/

function cleanUpQcFormItemOpenModuleForm(moduleFormName, moduleName, inputMode, rowid, parentId)
{
    cleanUpQcFormItemInputMode = inputMode;

    if(parentId === undefined || parentId==0){
        cleanUpQcFormObj = {};
    }
    else
    {
        cleanUpQcFormObj = $("#cleanUpQcFormList").getRowData(parentId);
    }

    if(rowid === undefined || rowid==0){
        cleanUpQcFormItemObj = {};
    }
    else
    {
        cleanUpQcFormItemObj = $("#cleanUpQcFormList_" + cleanUpQcFormObj.id + "_t").getRowData(rowid);
    }

    $('#'+moduleFormName).dialog('open');
}

function validateCleanUpQcFormItemForm()
{
    var validateFlag = true;

    if (!$("#cleanUpQcFormItemForm").validationEngine({returnIsValid:true}))
    {
        validateFlag = false;
        return validateFlag;
    }

    return validateFlag;
}

function submitCleanUpQcFormItemForm()
{
    if(!validateCleanUpQcFormItemForm()){
        return;
    }

    wacShowBlockUILoading("#cleanUpQcFormItemFormDialog", "处理中...");

    var extraParams = "data_format=json";
    var submitUrl;

    if(cleanUpQcFormItemInputMode == wacFormInputMode.add){
        submitUrl = BASE_URL + "cleanUpQcFormItem/add";
    }
    else{
        submitUrl = BASE_URL + "cleanUpQcFormItem/edit";
    }


    $.ajax({
        url: submitUrl,
//        url: BASE_URL + "test/ajaxTest" ,
        global: true,
        type: "GET",
        data: $("#cleanUpQcFormItemForm").serialize() + "&" + extraParams,
        dataType: "json",
        success: function(jsonData){
            submitCleanUpQcFormItemFormCallBack(jsonData);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            wacDebugLog("getFormData Error: " + $(this).dump()); // the options for this ajax request
        }
    });
}

function submitCleanUpQcFormItemFormCallBack(jsonData)
{
   if(jsonData.info.status == wacOperationStatus.Error)
   {
       wacShowTips(jsonData.info.error_info);
   }
   else
   {
       wacShowTips(jsonData.info.tips);
       $("#cleanUpQcFormList_" + cleanUpQcFormObj.id + "_t").trigger("reloadGrid");
   }

   wacHideBlockUI("#cleanUpQcFormItemFormDialog");

//   wacDebugLog($.dump(jsonData));
}

/**************  interaction section, end  ***************/